<?php
class Compare extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('product_model');
		
	}
	
	public function index(){
	}
	
	public function compare_items($ids){
		$data['backString'] = ($this->session->userdata('product_search_url') ? $this->session->userdata('product_search_url') : '');
	
		$ids_array = explode(",",$ids);
		
		$ids = '';
		foreach ($ids_array as $key=>$value){
			$ids .= "'" . $value . "',";
		}
		
		$ids = substr($ids, 0, -1);
		
		$search = array('id_in' => $ids);
		$search['group_by'] = 'products.id';
		
		$data['prodObj'] = new Product_model();
		
		$data['products'] = $this->product_model->getAll($search)->result();
		
		if (!empty($data['products'])){
			foreach ($data['products'] as $key=>$product){
				$data['products'][$key]->childs = $this->product_model->getAll(array('parent_id' => $product->id))->result();
			}
		}
		
		$this->template->add_js('application/assets/js/compare.js');
		$this->template->add_js('application/assets/js/jqueryPlugins/Rating/jquery.raty.js');
		$this->template->write('pageTitle', lang('compare_page_title',false));
		$this->template->write_view('content','front/compare.php',$data);
		$this->template->render();
		
	}
	
	function get_prod_details($id = 0){
		
		if (!$id){
			echo json_encode(array('status' => 1,'html' => ''));
			exit(1);
		}
		
		$exchange = new Exchange();
		$prodObj = new Product_model();
		$product = $prodObj->get_by_id($id);

		$session_country = $this->session->userdata('selected_country');
		$country = $session_country ? $session_country : $this->config->item('def_country');
		
		$session_currency = $this->session->userdata('selected_currency');
		$sel_currency = $session_currency ? $session_currency : $this->config->item('def_currency');
		
		switch ($country){
			case 1:
				$field = 'size_uk';
				break;
			case 6:
			case 43:
				$field = 'size_usa';
				break;
			case 7:
			case 21:
				$field = 'size_japan';
				break;
			default:
				$field = 'size_eu';
				break;
		}
		
		$html = '';
		
		$html .= '<div class="cell">' . round($product->carat_weight,2) . ' CT</div>';
		$html .= '<div class="cell">' . $product->clarity . '</div>';
		$html .= '<div class="cell">' . $product->setting .'</div>';
		$html .= '<div class="cell">' . $product->shape . '</div>';
		$html .= '<div class="cell">' . $product->diamond_size_text . '</div>';
		$html .= '<div class="cell">' . $product->ring_band_width . '</div>';

		$sizes = $prodObj->getRingSizesByProductId($product->id,$field)->result();
		$sizesRow = '';
        foreach ($sizes as $row){
        	$sizesRow .= '<option value="' . $row->size_field . '">' . $row->size_field . '</option>';
		}
		$html .= '<div class="cell">
			<select id="ring_size_' . $product->id . '" name="ring_size" class="required">
                	<option value="" selected="selected"></option>      
                	' . $sizesRow . '                                                   
			</select>
		</div>';
		$html .= '<div class="cell">' . ($product->certified ? "<img src='" . IMAGES_URL . "icons/certificate_check.png' />" : '') . '</div>';
		$html .= '<div class="cell old-price">' . $exchange->transformPrice($product->price,$sel_currency,true, $product->id) . ' ' . $sel_currency . '</div>';
		$html .= '<div class="cell our-price">' . $exchange->transformPrice($product->special_price,$sel_currency,true, $product->id, false, false, $product->promotion_value) . ' ' . $sel_currency . '</div>';
		$html .= '<div class="cell buttons-container">
			<div class="add-to-basket" onclick="javascript:addToBasket(' . $product->id . ')">' . lang('add_to_basket_compare') . '</div>
			<div class="remove-btn" onclick="javascript:removeCol(' . $product->parent_id . ')">' . lang('remove_compare') . '</div>
		</div>';
		
		echo json_encode(array('status' => 1, 'html' => $html, 'id' => $product->id));
	}
	
	function send_page(){
		$this->load->library('email');
		
		$content = '<style type="text/css">' . file_get_contents(ASSETS_PATH . 'css/main.css') . '</style>';
		$content .= $this->input->post('html');
		$name = $this->input->post('name');
		$email = $this->input->post('email');
		
		$this->email->initialize(array('mailtype' => 'html', 'protocol' => 'sendmail'));
		
		$this->email->from($this->config->item('contact_email_address'), $this->config->item('office_name') );
        $this->email->to($email,$name);
        $this->email->mailtype = 'html';
        $this->email->subject(lang('compare_page'));
        $this->email->message($content);
        if ($this->email->send()){
        		$response['status'] = 1;
        }
        else{
                $response['status'] = 0;
        }
        
        echo json_encode($response);
	}
}